package javase.javase01.exercise03;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author: Gan
 * @date 2021/4/16 0016 20:06
 * @description: 3.利用缓冲流去复制文本文件  图片文件  视频文件 (2种方式 单字节  多字节)
 **/
public class CopyTxt {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream(".\\a.txt");
        FileOutputStream out = new FileOutputStream(".\\b.txt");
        copy1(in, out);
        copy2(in, out);
        in.close();
        out.close();
    }

    private static void copy2(FileInputStream in, FileOutputStream out) throws IOException {
        int readCount;
        byte[] bytes = new byte[1024];
        long start = System.currentTimeMillis();
        while ((readCount=in.read(bytes))!=-1){
            out.write(bytes,0,readCount);
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时" + (end - start) + "ms");
    }

    private static void copy1(FileInputStream in, FileOutputStream out) throws IOException {
        int readDate;
        long start = System.currentTimeMillis();
        while ((readDate = in.read()) != -1) {
            out.write(readDate);
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时" + (end - start) + "ms");
    }
}
